﻿using System;
using System.IO;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp;
using Volo.Abp.AspNetCore.Mvc;

namespace PublicGateway.Host.Controllers
{
    /// <summary>
    /// 上传控制器
    /// </summary>
    [Authorize]
    [RemoteService]
    [Area("commonManagement")]
    [Route("api/commonManagement/upload")]
    public class UploadController : AbpController
    {
        // 1.7.1 通用上传接口
        [HttpPost]
        [Route("")]
        public IActionResult Index(IFormFile file)
        {
            var fileName = $"{Guid.NewGuid().ToString("N")}{Path.GetExtension(file.FileName)}";
            var fileUrl = $"/Uploads/{fileName}";
            var filePath = $"{Directory.GetCurrentDirectory()}{fileUrl}";
            using (FileStream fs = new FileStream(filePath, FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            var response = UploadResponse.Successful(fileName, fileUrl);
            return new JsonResult(response);
        }
    }
}